// pages/uploadpage/uploadpage.js
import {ajax} from "../../utils/httpRequest";
import {URI_WANSHANGIMG,URI_UPLOADSELFIMG} from "../../utils/api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '/images/default-head.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //-------------上传图片--人脸识别---------------------------------------------------
  bindPhone: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        wx.showToast({
          title: '人脸正在识别中请稍后！！',
          icon: 'loading',
          duration: 5000,
          mask: false
        })
        var tempFilePaths = res.tempFilePaths[0];
        ajax.get('https://api.cloudcpc.com/home/Qiniu/upload', {}, {}, res => {
          //console.log(res.token);
          that.comenUploadFun(res.token, tempFilePaths)
        })
      }
    })
  },
  comenUploadFun(token, path) {
    let that = this;
    wx.uploadFile({
      url: 'https://upload.qiniup.com',
      // url: 'https://up-z2.qiniup.com',
      filePath: path,
      name: 'file',
      header: {
        'Content-Type': 'multipart/form-data'
      },
      formData: {
        token: token
      },
      success: (res) => {
        var data = JSON.parse(res.data);
        // var upladurl = 'http://pjapi6s2a.bkt.clouddn.com/' + data.key;
        var upladurl = 'http://oss.co-links.com/' + data.key;
        this.postImgInfo(upladurl);
      },
      fail: (error) => {
        wx.showModal({
          title: "提示",
          content: '您的人脸识别失败，请重试!',
          showCancel: false,
          confirmColor: '#0d77e4',
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    })
  },

  postImgInfo(url) {
    ajax.post(URI_UPLOADSELFIMG, {
      uid:wx.getStorageSync('uid'),
      url: url
    }, {}, res => {
      wx.hideToast();
      console.log(res);
      if (res.type == 3) {
        ajax.post(URI_WANSHANGIMG,{
          dangyaun_id:wx.getStorageSync('dangyuan_id'),
          facedata:url
        },{},res=>{
            // console.log(res);
          let that = this;
          this.setData({
            url: url
          });
          wx.showModal({
            title: '提示',
            content: '您的人脸识别已经成功！',
            showCancel: false,
            confirmColor: '#0d77e4',
            confirmText: '立即查看',
            success: (res) => {
              if (res.confirm) {
                wx.reLaunch({
                  url: '/pages/user_manage/user_manage',
                })
              }
            }
          })
        });

      } else {
        wx.showModal({
          title: "提示",
          content: '您的人脸识别失败，请重试。',
          showCancel: false,
          confirmColor: '#0d77e4',
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})